//
//  ShowMenuView.m
//  EMMApp
//
//  Created by Mac on 16/5/10.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "TimerShowMenuView.h"

@interface TimerShowMenuView () {
    NSString *timer;
}

@property (nonatomic, strong) UIDatePicker *pickerView;         //显示时间滚动界面

@end

@implementation TimerShowMenuView

- (id)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
         NSDate *now = [[NSDate alloc]init];
        [_pickerView setDate:now animated:NO];
        [self showUI];
    }
    return self;
}

#pragma 创建视图初始化
- (void)showUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT)];
    view.backgroundColor = [UIColor clearColor];
    view.userInteractionEnabled = YES;
    [self addSubview:view];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [view addGestureRecognizer:tap];
    
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, EMM_HEIGHT-275, EMM_WIDTH, 275)];
    subView.backgroundColor = [UIColor whiteColor];
    subView.userInteractionEnabled = YES;
    [self addSubview:subView];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 54)];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"选择时间";
    title.font = [UIFont systemFontOfSize:16];
    [subView addSubview:title];
    
    UIView *hxView = [[UIView alloc] initWithFrame:CGRectMake(0, 54, EMM_WIDTH, 1)];
    hxView.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [subView addSubview:hxView];
    UIImageView *imaV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 17.5, 20, 20)];
    imaV.image = [UIImage imageNamed:@"close"];
    [subView addSubview:imaV];
    
    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    cancel.frame = CGRectMake(0, 0, 60, 55);
//    [cancel setImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [cancel addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
    [subView addSubview:cancel];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(subView.frame.size.width - 65, 7.5, 50, 40);
//    btn.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
//    btn.layer.masksToBounds = YES;
//    btn.layer.cornerRadius = 5;
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitleColor:[UIColor colorFromHexCode:@"#1A1A1A"] forState:UIControlStateNormal];
    [subView addSubview:btn];
    
    _pickerView  = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, EMM_WIDTH, subView.frame.size.height-55)];
    _pickerView.datePickerMode = UIDatePickerModeDate;
    [subView addSubview:_pickerView];
   
}

#pragma 点击阴影视图消失
- (void)tapClick:(UITapGestureRecognizer *)tap {
    if (_timerBlock) {
        _timerBlock(@"");
    }
}

#pragma 取消按钮
- (void)cancelClick:(UIButton *)btn {
    if (_timerBlock) {
        _timerBlock(@"");
    }
}

#pragma 保存按钮
- (void)btnclick:(UIButton *)btn {
    NSDate *selected = [_pickerView date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    timer = [dateFormatter stringFromDate:selected];
    if (_timerBlock) {
        _timerBlock(timer);
    }
}



@end
